<template>
    <div>
        <div class="editable-cell">
            <div v-if="editable" class="editable-cell-input-wrapper">
                <Row type="flex" justify="center" align="middle">
                    <Col span="19">
                        <a-input :value="value" @change="handleChange" @pressEnter="check"/>
                    </Col>
                    <Col span="5">
                        <a-icon
                                type="check"
                                class="editable-cell-icon-check"
                                @click="check"/>
                        <a-icon type="close" @click="cancel"/>
                    </Col>
                </Row>
            </div>
            <div v-else class="editable-cell-text-wrapper">
                {{ value || ' ' }}
                <a-icon type="edit" class="editable-cell-icon" @click="edit"/>
            </div>
        </div>
    </div>
</template>

<script>
    import 'ant-design-vue/dist/antd.css';
    import {Input, Icon} from 'ant-design-vue';
    import Vue from 'vue'

    Vue.use(Input)
    Vue.use(Icon)
    export default {
        name: "EditableCell",
        props: {
            text: String,
        },
        data() {
            return {
                str: '',
                value: this.text,
                editable: false,
                state:''
            };
        },
        methods: {
            handleChange(e) {
                const value = e.target.value;
                this.value = value;
            },
            check() {
                this.editable = false;
                this.$emit('change', this.value);
            },
            edit() {
                this.str = this.value;
                this.editable = true;
            },
            cancel() {
                this.value = this.str;
                this.editable = false;
            },
        },
    }
</script>

<style scoped>
    .editable-cell-icon {
        float: right;
    }
</style>
